﻿<?PHP
//session_start();
include "connectdb.php";
require_once('VideoPlayer.class.php');

//DECLARE VARIABLE
$username = $_SESSION['sess_Username'];
$password = $_SESSION['sess_Password'];
$emp_id = $_SESSION['sess_ID'];

$txtfile = $_POST["txtfile"];

$vdosrname = $_POST["txtsrname"];
$vdoauthor = $_POST["txtauthor"];

$vdoyear   = $_POST["txtyear"];
$vdolength = $_POST["txthour"].":".$_POST["txtmin"].":".$_POST["txtsec"];

$vdocover  = isset($_POST["txtcover"]) ?$_POST["txtcover"] :"pic/no_image.gif" ;
$vdocover = str_replace("\\\\","/",$vdocover); //แปลงจาก ( \\ เป็น \ ) 

$cb_manual_path = $_POST["cb_manualpath"];
$manual_path = $_POST["txtmanualpath"];
$manual_path = str_replace("\\\\","/",$manual_path);

if ($cb_manual_path == "0"){
	$use_path =  $txtfile;
}else{
	$use_path =  $manual_path;
}

$b = explode("\\\\", $use_path);
$count = count($b);

$filename = $b[$count-1]; 		/////////////
$subfolder = $b[$count-2]; 		/////////////
$mainfolder = $b[$count-3]; 	/////////////

$c = explode(".",$filename);
$count_cover = count($c);
$filename_cover = $c[$count_cover-2]."_cover_1";
if($c[$count_cover-2] == ""){$filename_cover = "";}

$pathcover = $mainfolder."/".$subfolder;
$filenewcon = strrchr($_POST["txtcover"],'.');

$vdoname = $_POST["txtname"];
$desc = $_POST["txtdesc"];
$keyword = str_replace(" ","",trim($_POST["txtkeyword"]));

$today = Date("Y/m/d");
$url = base64_encode("$use_path");
$vdo_recommend = $cb_recommend;
if($vdo_recommend!="1"){$vdo_recommend = "0";}

$path = str_replace("\\\\","\\",$_POST["txtfile"]);

$path = dirname($path)."\\";

$SQL_SELECT_DUPNAME = "SELECT count(vdo_id) FROM ag_vdodetail where vdo_name = '$vdoname' ;";
$dupname = mysql_query($SQL_SELECT_DUPNAME);
$dupname2 = mysql_result($dupname,0);
$dupname2 = (int)$dupname2;

$_SESSION['video'] = $use_path;
$_SESSION['extension'] = NULL;
$_SESSION['accepted_extensions'] = array('wmv', 'asx','flv','mov','rmv','rmvb','swf', 'mpg', 'mpeg', 'mp4', 'avi');

    function verifyVideo() {
		  	$_SESSION['video'] = isset( $_SESSION['video'] ) ? $_SESSION['video'] : false;
		  	if( !$_SESSION['video'] || $_SESSION['video'] == '' ){
					return false;
			 } else {
					return true;
			 }
    }

    function verifyExtension() {
			$_SESSION['extension'] = NULL;
			$_SESSION['accepted_extensions'] = array('wmv', 'asx','flv','mov','rmv','rmvb','swf', 'mpg', 'mpeg', 'mp4', 'avi');
			$_SESSION['extension'] = end(explode('.', $_SESSION['video']));
		    if (!in_array($_SESSION['extension'], $_SESSION['accepted_extensions'])) {
					return false;
			} else {
					return true;
			}
    }

		if( verifyVideo() == true && verifyExtension() == true ) {
		
				for ($i = 0; $i<count($mediatype_array); $i++) {
				$mediatypeid = $mediatype_array[$i];
				}
		
				$direc = dir("$path"); 
		
				if($show_dir = $direc -> read()) 
				{ 

						//// ทำการเพิ่มข้อมูล file multimedia ////
						$SQL_SELECT_MAXVDO = "SELECT MAX(vdo_id) FROM ag_vdodetail;";
						$max = mysql_query($SQL_SELECT_MAXVDO);
						$max2 = mysql_result($max,0);
						$newid = $max2+1;
						
$SQL_INSERT_VDO = "INSERT INTO ag_vdodetail VALUES('$newid'
																					, '$vdosrname'
																					, '$vdoname'
																					, '$desc'
																					, '$vdoauthor'
																					, '$vdoyear'
																					, '$vdocover'
																					, '$vdolength'
																					, '$url'
																					, ''
																					, ''
																					, ''
																					, '$today'
																					, '$emp_id'
																					, '$mediatypeid'
																					, '$keyword'
																					, '$vdo_recommend'
																					, '$vdo_viewrecent');";
						$result = (mysql_db_query($dbname,$SQL_INSERT_VDO) or die ("ไม่สามารถเพิ่มไฟล์นี้ได้"));
						
						$SQL_SELECT_MAXVDO = "SELECT MAX(vdo_id) FROM ag_vdodetail;";
						$resultmax = mysql_query($SQL_SELECT_MAXVDO);
						$resultmax2 = mysql_result($resultmax,0);
						
						// ทำการเพิ่มข้อมูลลงตาราง ag_vdotypedetail เพื่อบอกว่า vdo แต่ละเรื่องมีเนื้อหาอยู่ในหมวดใดบ้าง ////
						for ($i=0;$i<count($vdotype_array);$i++) {
							$vdotypeid = $vdotype_array[$i];

							//XXX REMOVE THIS
							echo '<br>VDO-TYPE-ID = '.$vdotypeid;

							$SQL_INSERT_TYPEDETAIL = "INSERT INTO ag_vdotypedetail VALUES('$resultmax2','$vdotypeid');";
							$result_vdotypedetail = (mysql_db_query($dbname,$SQL_INSERT_TYPEDETAIL) or die ("ไม่สามารถเพิ่มประเภทเนื้อหาของสื่อเรื่องนี้ได้"));
						}
						
						?>
						<script>
							alert("เพิ่มรายการสื่อมัลติมีเดียเรียบร้อยแล้ว");
						</script>
						<meta http-equiv='refresh' content='0;URL=vdo_list_edit.php'>
						<?	
				
				} 
				$direc -> Close();  
		}else{
		
				?>
				<script>
					alert("กรุณาตรวจสอบไฟล์ \nประเภทไฟล์ที่สนับสนุน wmv , wma ")
					history.back();
				</script>
				<?
		}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"/>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
</body>
</html>
